package com.greenyoung.pwiki.uaa.component;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.Sets;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 添加controller之外的接口，这里是添加oauth接口
 */
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {

    @Override
    public List<ApiDescription> apply(DocumentationContext documentationContext) {
        List<ApiDescription> apiDescriptions = new ArrayList<>();
        apiDescriptions.add(oauthToken());

        return apiDescriptions;
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return DocumentationType.SWAGGER_2.equals(documentationType);
    }

    private ApiDescription oauthToken() {

        Parameter parameter1 = new ParameterBuilder()
                //参数名称
                .name("client_id")
                //参数描述
                .description("客户端id")
                //参数默认值
                .defaultValue("c1")
                //参数数据类型
                .type(new TypeResolver().resolve(String.class))
                //参数类型
                .parameterType("query")
                .parameterAccess("access")
                //是否必填
                .required(true)
                //参数数据类型
                .modelRef(new ModelRef("string"))
                .build();
        Parameter parameter2 = new ParameterBuilder()
                .name("client_secret")
                .description("客户端密码")
                .defaultValue("123")
                .type(new TypeResolver().resolve(String.class))
                .parameterType("query")
                .parameterAccess("access")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();
        Parameter parameter3 = new ParameterBuilder()
                .name("grant_type")
                .description("oauth2鉴权方式，如password")
                .type(new TypeResolver().resolve(String.class))
                .defaultValue("password")
                .parameterType("query")
                .parameterAccess("access")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();
        Parameter parameter4 = new ParameterBuilder()
                .name("username")
                .description("用户名")
                .type(new TypeResolver().resolve(String.class))
                .defaultValue("ph")
                .parameterType("query")
                .parameterAccess("access")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();
        Parameter parameter5 = new ParameterBuilder()
                .name("password")
                .description("用户密码")
                .type(new TypeResolver().resolve(String.class))
                .defaultValue("123")
                .parameterType("query")
                .parameterAccess("access")
                .required(true)
                .modelRef(new ModelRef("string"))
                .build();

        OperationBuilder operationBuilder1 = new OperationBuilder(new CachingOperationNameGenerator());
        operationBuilder1.method(HttpMethod.POST)
                .produces(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                .summary("获取token")
                //方法描述
                .notes("获取token")
                //归类标签
                .tags(Sets.newHashSet("Token"))
                .parameters(Arrays.asList(parameter1, parameter2, parameter3, parameter4, parameter5));

        ApiDescription apiDescription = new ApiDescription("/uaa/oauth/token", "获取token",
                Arrays.asList(operationBuilder1.build()), false);
        return apiDescription;

    }
}
